package cn.ecut.dao.user;

import cn.ecut.dao.BaseDao;
import cn.ecut.pojo.User;
import com.mysql.fabric.xmlrpc.base.Param;
import com.mysql.jdbc.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author ruanxin
 */
public class UserDaoImpl implements UserDao {
    //获取登录用户
    @Override
    public User getLoginUser(Connection connection, String userCode) throws SQLException {
        User user = null;
        ResultSet rs = null;
        PreparedStatement preparedStatement = null;

        if (null != connection) {
            String sql = "select * from smbms_user where userCode=?";
            Object[] params = {userCode};
            rs = BaseDao.execute(connection, preparedStatement, rs, sql, params);
            if (rs.next()) {
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUserCode(rs.getString("userCode"));
                user.setUserName(rs.getString("userName"));
                user.setUserPassword(rs.getString("userPassword"));
                user.setGender(rs.getInt("gender"));
                user.setBirthday(rs.getDate("birthday"));
                user.setPhone(rs.getString("phone"));
                user.setAddress(rs.getString("address"));
                user.setUserRole(rs.getInt("userRole"));
                user.setCreatedBy(rs.getInt("createdBy"));
                user.setCreationDate(rs.getDate("creationDate"));
                user.setModifyDate(rs.getDate("modifyDate"));
                user.setModifyBy(rs.getInt("modifyBy"));
            }
            BaseDao.close(null, rs, preparedStatement);
        }

        return user;
    }

    //修改当前用户密码
    @Override
    public int upateUserPassword(Connection connection, int id, String password) throws SQLException {
        PreparedStatement preparedStatement = null;
        int execute = 0;
        System.out.println("UserDao" + password);
        if (connection != null) {
            String sql = "update smbms_user set userPassword = ? where id = ?";
            Object[] params = {password, id};
            execute = BaseDao.execute(connection, preparedStatement, sql, params);
            BaseDao.close(null, null, preparedStatement);
        }

        return execute;
    }

    //根据用户名和用户角色查询用户总数
    @Override
    public int getUserCount(Connection connection, String userName, int userRole) throws SQLException {
        PreparedStatement psmt = null;
        ResultSet rs = null;
        int execute = 0;

        if (connection != null) {
            StringBuffer sql = new StringBuffer();
            sql.append("select count(1) as count from smbms_user u,smbms_role r where u.userRole = r.id");
            ArrayList<Object> list = new <Object>ArrayList();

            //如果用户名有值就加上根据用户名模糊查询
            if (!StringUtils.isNullOrEmpty(userName)) {
                sql.append(" and u.userName like ?");
                list.add("%" + userName + "%");
            }

            //如果用户角色选择了就加上根据用户角色查询
            if (userRole > 0) {
                sql.append(" and r.id= ?");
                list.add(userRole);
            }

            Object[] param = list.toArray();
            System.out.println("UserDaoImpl  sql---->" + sql.toString());

            rs = BaseDao.execute(connection, psmt, rs, sql.toString(), param);
            if (rs.next()) {
                execute = rs.getInt("count");
            }
        }

        BaseDao.close(null, rs, psmt);

        return execute;
    }

    @Override
    public List<User> getUserList(Connection connection, String userName, int userRole,int currentPageNo, int pageSize) throws SQLException {
        PreparedStatement psmt = null;
        ResultSet rs = null;
        ArrayList<User> userList = new ArrayList<>();

        if (connection != null) {
            StringBuffer sql = new StringBuffer();
            sql.append("select u.*,r.roleName as userRoleName from smbms_user u,smbms_role r where u.userRole = r.id");

            ArrayList<Object> list = new <Object>ArrayList();

            //如果用户名有值就加上根据用户名模糊查询
            if (!StringUtils.isNullOrEmpty(userName)) {
                sql.append(" and u.userName like ?");
                list.add("%" + userName + "%");
            }

            //如果用户角色选择了就加上根据用户角色查询
            if (userRole > 0) {
                sql.append(" and u.userRole = ?");
                list.add(userRole);
            }

            //处理分页
            /*
            * 当前页  （当前页-1）*页面大小
            * 0，5       1   0   01234
            * 5，5       2   5   56789
            * 10，5      3   10
            * */
            sql.append(" order by creationDate DESC limit ?,?");
            currentPageNo=(currentPageNo-1)*pageSize;
            list.add(currentPageNo);
            list.add(pageSize);

            Object[] param = list.toArray();
            System.out.println("UserDaoImpl  sql---->" + sql.toString());
            rs = BaseDao.execute(connection, psmt, rs, sql.toString(), param);
            while(rs.next()){
                User _user = new User();
                _user.setId(rs.getInt("id"));
                _user.setUserCode(rs.getString("userCode"));
                _user.setUserName(rs.getString("userName"));
                _user.setGender(rs.getInt("gender"));
                _user.setBirthday(rs.getDate("birthday"));
                _user.setPhone(rs.getString("phone"));
                _user.setUserRole(rs.getInt("userRole"));
                _user.setUserRoleName(rs.getString("userRoleName"));
                _user.setAge(_user.age());
                userList.add(_user);
            }

            BaseDao.close(null,rs,psmt);
        }


        return userList;
    }
}
